<template>
    <el-dialog :visible.sync="visible" title="商品详情" :close-on-click-modal="false"
               :close-on-press-escape="false">
        <el-form :model="dataForm" ref="dataForm"
                 :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
            <el-form-item >
                <!-- 富文本编辑器, 容器 -->
                <div id="J_quillEditor"></div>
                <!-- 自定义上传图片功能 (使用element upload组件) -->
                <!--<el-upload
                        :action="uploadUrl"
                        :show-file-list="false"
                        :before-upload="uploadBeforeUploadHandle"
                        :on-success="uploadSuccessHandle"
                        style="display: none;">
                    <el-button ref="uploadBtn" type="primary" size="small">{{ $t('upload.button') }}</el-button>
                </el-upload>-->
            </el-form-item>
        </el-form>
        <template slot="footer">
            <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
        </template>
    </el-dialog>
</template>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
<script>
    import debounce from 'lodash/debounce'
    import Cookies from "js-cookie";
    import 'quill/dist/quill.snow.css'
    import Quill from 'quill'

    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: '',
                    commodityName: '',
                    commodityMarketPrice: '',
                    shopPrice: '',
                    commodityPic: '',
                    commodityDetails: ''
                },
                //富文本
                quillEditor: null,
                quillEditorToolbarOptions: [
                    ['bold', 'italic', 'underline', 'strike'],
                    ['blockquote', 'code-block', 'image'],
                    [{'header': 1}, {'header': 2}],
                    [{'list': 'ordered'}, {'list': 'bullet'}],
                    [{'script': 'sub'}, {'script': 'super'}],
                    [{'indent': '-1'}, {'indent': '+1'}],
                    [{'direction': 'rtl'}],
                    [{'size': ['small', false, 'large', 'huge']}],
                    [{'header': [1, 2, 3, 4, 5, 6, false]}],
                    [{'color': []}, {'background': []}],
                    [{'font': []}],
                    [{'align': []}],
                    ['clean']
                ],
                uploadUrl: ''
            }
        },
        methods: {
            init() {
                this.visible = true
                this.$nextTick(() => {
                    if (this.quillEditor) {
                        this.quillEditor.deleteText(0, this.quillEditor.getLength())
                    } else {
                        this.quillEditorHandle()
                    }
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        this.getInfo()
                    }
                })
            },
            // 富文本编辑器
            quillEditorHandle () {
                this.quillEditor = new Quill('#J_quillEditor', {

                })
                /*// 自定义上传图片功能 (使用element upload组件)
                this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/sys/oss/myUpload?token=${Cookies.get('token')}`
                this.quillEditor.getModule('toolbar').addHandler('image', () => {
                    this.$refs.uploadBtn.$el.click()
                })
                // 监听内容变化，动态赋值
                this.quillEditor.on('text-change', () => {
                    this.dataForm.commodityDetails = this.quillEditor.root.innerHTML
                })*/
            },
           /* // 上传图片之前
            uploadBeforeUploadHandle (file) {
                if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
                    this.$message.error('只支持jpg、png、gif格式的图片！')
                    return false
                }
            },
            // 上传图片成功
            uploadSuccessHandle (res, file, fileList) {
                if (res.code !== 0) {
                    return this.$message.error(res.msg)
                }
                this.quillEditor.insertEmbed(this.quillEditor.getSelection().index, 'image', this.imgUrl + res.data.src)
            },*/
            // 获取信息
            getInfo() {
                this.$http.get(`/demo/xlscommodity/${this.dataForm.id}`).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.dataForm = {
                        ...this.dataForm,
                        ...res.data
                    }
                    this.quillEditor.root.innerHTML =  this.dataForm.commodityDetails
                }).catch(() => {
                })
            }
        }
    }
</script>
